package com.nl.controller.web.controller;


import com.github.pagehelper.PageInfo;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.model.Response;

import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.bean.SmsBaseStation;
import com.nl.controller.web.bean.SmsUsers;

import com.nl.controller.web.global.GlobalConst;
import com.nl.controller.web.service.SmsBaseStationService;

import com.nl.controller.web.util.FileUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@RequestMapping("smsBaseStation")
@RestController
public class SmsBaseStationController {

    @Autowired
    private SmsBaseStationService smsBaseStationService;

    @ApiOperation(value = "查询模板信息", notes = "分页查询模板信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, dataType = "String"),
            @ApiImplicitParam(name = "cityId", value = "城市编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "mgrId", value = "项目编号", required = true, dataType = "String"),
    })
    @PostMapping("find")
    public Response findBaseStation(
            @RequestParam(value = "page", required = true) int page,
            @RequestParam(value = "limit", required = true) int limit,
            @RequestParam(value = "cityId", required = false) String cityId,
            @RequestParam(value = "mgrId", required = false) String mgrId,
            @RequestParam(value = "mgrName", required = false) String mgrName,
            @RequestParam Map<String, Object> paramMap
    ) {
        if ("".equals(cityId) || cityId == null) {
            throw new SmsException("请选择查询条件，开始查询");
        } else {
            PageInfo<SmsBaseStation> pageInfo = smsBaseStationService.findSmsBaseStation(page, limit, cityId, mgrId,mgrName);
            log.info(" 查询基站信息：{}", paramMap.toString());
            log.debug("查询基站信息返回参数：{}", pageInfo.getList().toString());
            return ResponseUtil.sucess(pageInfo.getList(), pageInfo.getTotal());
        }
    }

    @RequestMapping(value = "/modeldownload", method = RequestMethod.GET)
    public void download(HttpServletResponse res) throws Exception {
        String fileName = "BaseStation.csv";
        FileUtil.download(fileName,res);
    }

    @RequestMapping(value = "/apidownload", method = RequestMethod.GET)
    public void download2(HttpServletResponse res) throws Exception {
        String fileName = "BaseStationImportApi.docx";
        FileUtil.download(fileName,res);
    }
    //处理文件上传
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public Response uploadImg(@RequestParam("file") MultipartFile file,
                                         HttpServletRequest request,
                                         @RequestParam(value = "cityId", required = true) String cityId,
                                         @RequestParam(value = "mgrId", required = true) String mgrId) {

        Map<String, Object> map = new HashMap<>();
        SmsUsers user = (SmsUsers) request.getSession().getAttribute("user");
        String now = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        String fileName = user.getUserId() + now + mgrId;
        String path = GlobalConst.FILE_STATION_PATH;
        try {
            FileUtil.fileUpload(file,request,path,fileName); //文件上传
            log.info("文件上传成功");
            log.info(mgrId+"----"+cityId);
        }catch (Exception e){
            e.printStackTrace();
            log.error("文件上传异常");
            return ResponseUtil.error(1, "文件上传异常");
        }
            try {
                List list = smsBaseStationService.readCsv(path + "/" + fileName, mgrId, cityId); //文件解析
                return ResponseUtil.sucess(list);
            } catch (Exception e) {
                e.printStackTrace();
                map.put("code", 1);
                log.error("数据异常请检查");
                return ResponseUtil.error(1, "数据异常请检查");
            }
        }
}